<?php
	
	require_once '../config/common.php';
	require_once '../../util/util.class.php';
	
	class Dbini{
		protected $db; //数据库连接
		
		/**
		 * 初始化数据库连接
		 * @param array $dbConfArray
		 * @throws PDOException
		 */
		public function __construct(array $dbConfArray=null){
			try{
				if(count($dbConfArray) <= 0){
					global $dbArray;
					$dbConfArray = $dbArray;
				}
				$charset = isset($dbConfArray['charset']) && !empty($dbConfArray['charset']) ? trim($dbConfArray['charset']) : 'utf-8';
				$dsn = 'mysql:dbname='.$dbConfArray['dbname'].';host='.$dbConfArray['host'].';charset='.$charset;
				$db = new PDO($dsn, $dbConfArray['username'], $dbConfArray['password']);
				if(!is_object($db) || empty($db)){
					throw new PDOException('数据库连接出错');
				}
				$this -> db = $db;
			}catch(PDOException $e){
				Util::writelog('数据库连接出错，错误代码：'.$e->getCode().'，跟踪路径：'.$e->getTraceAsString());
				exit('error!!');
			}
		}
		
		/**
		 * 关闭数据库连接
		 */
		public function __destruct(){
			if(isset($this -> db)){
				$this -> db = null;
				unset($this -> db);
			}
		}
		
	}
